//
//  JBAccountManager.m
//  SC
//
//  Created by YongbinZhang on 10/25/13.
//  Copyright (c) 2013 ZhangYongbin(JustBen). All rights reserved.
//

#import "JBAccountManager.h"
#import "JBUserDefaultServiceCenter.h"




static NSString *defaultUserId = @"0";

static NSString *const kLogined = @"logined";

static NSString *const kAccountLoginType = @"accountLoginType";
static NSString *const kAccountUserId = @"accountUserId";
static NSString *const kAccountUserName = @"accountUserName";
static NSString *const kAccountPassword = @"accountPassword";
static NSString *const KAccountUserNickName =@"accountUserNickName";
static NSString *const KAccountUserHeadImgurl =@"accountUserHeadImgurl";
static NSString *const KAccountUserSign =@"accountUserSign";
static NSString *const KAccountUserGender =@"accountUserGender";

static NSString *const KAccountUserProvinceName =@"accountUserProvinceName";
static NSString *const KAccountUserProvinceCode =@"accountUserProvinceCode";
static NSString *const KAccountUserCityName =@"accountUserCityName";
static NSString *const KAccountUserCityCode =@"accountUserCityCode";

@interface JBAccountManager ()

@property (nonatomic, assign) BOOL logined;

@property (nonatomic, assign) LoginType accountLoginType;
@property (nonatomic, copy) NSString *accountUserId;
@property (nonatomic, copy) NSString *accountUserName;
@property (nonatomic, copy) NSString *accountPassword;
@property (nonatomic, copy) NSString *accountUserNickName;
@property (nonatomic, copy) NSString *accountUserHeadImgurl;
@property (nonatomic, copy) NSString *accountUserSign;
@property (nonatomic, copy) NSString *accountUserGender;

@property (nonatomic, copy) NSString *accountUserProvinceName;
@property (nonatomic, copy) NSString *accountUserProvinceCode;
@property (nonatomic, copy) NSString *accountUserCityName;
@property (nonatomic, copy) NSString *accountUserCityCode;
@end


@implementation JBAccountManager


#pragma mark - Class Methods
+ (JBAccountManager *)sharedAccountManager
{
    static JBAccountManager *sharedAccountManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedAccountManager = [[self alloc] init];
    });
    
    return sharedAccountManager;
}


#pragma mark - init
- (id)init
{
    self = [super init];
    if (self) {
        _logined = [[JBUserDefaultServiceCenter getUserDefaultValueByKey:kLogined] boolValue];
        if (_logined) {
            _accountLoginType = ((NSNumber *)[JBUserDefaultServiceCenter getUserDefaultValueByKey:kAccountLoginType]).integerValue;
            _accountUserId = [JBUserDefaultServiceCenter getUserDefaultValueByKey:kAccountUserId];
            _accountUserName = [JBUserDefaultServiceCenter getUserDefaultValueByKey:kAccountUserName];
            _accountPassword = [JBUserDefaultServiceCenter getUserDefaultValueByKey:kAccountPassword];
            _accountUserNickName = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserNickName];
            _accountUserHeadImgurl = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserHeadImgurl];
            _accountUserSign = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserSign];
            _accountUserGender = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserGender];
            
            _accountUserProvinceName = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserProvinceName];
            _accountUserProvinceCode = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserProvinceCode];
            _accountUserCityName = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserCityName];
            _accountUserCityCode = [JBUserDefaultServiceCenter getUserDefaultValueByKey:KAccountUserCityCode];
        } else {
            _accountUserId = defaultUserId;
        }
    }
    
    return self;
}


#pragma mark - Object Methods

- (void)loginWithType:(LoginType)type userName:(NSString *)userName password:(NSString *)password userId:(NSString *)userId nickName:(NSString *)nickName headImgurl:(NSString *)headImgurl sign:(NSString *)sign gender:(NSString *)gender provinceName:(NSString *)provinceName provinceCode:(NSString *)provinceCode cityName:(NSString *)cityName cityCode:(NSString *)cityCode
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if (userId) {
            self.accountLoginType = type;
            self.accountUserId = userId;
            self.accountUserName = userName;
            self.accountPassword = password;
            
            self.accountUserNickName = nickName;
            self.accountUserHeadImgurl = headImgurl;
            self.accountUserSign = sign;
            self.accountUserGender = gender;
            self.accountUserProvinceName = provinceName;
            self.accountUserProvinceCode = provinceCode;
            self.accountUserCityName = cityName;
            self.accountUserCityCode = cityCode;
            
            
            self.logined = YES;
            
            [JBUserDefaultServiceCenter setUserDefaultValue:[NSNumber numberWithBool:YES] WithKey:kLogined];
            [JBUserDefaultServiceCenter setUserDefaultValue:[NSNumber numberWithInteger:self.accountLoginType] WithKey:kAccountLoginType];
            [JBUserDefaultServiceCenter setUserDefaultValue:userId WithKey:kAccountUserId];
            [JBUserDefaultServiceCenter setUserDefaultValue:userName WithKey:kAccountUserName];
            [JBUserDefaultServiceCenter setUserDefaultValue:password WithKey:kAccountPassword];
            
            [JBUserDefaultServiceCenter setUserDefaultValue:nickName WithKey:KAccountUserNickName];
            [JBUserDefaultServiceCenter setUserDefaultValue:headImgurl WithKey:KAccountUserHeadImgurl];
            [JBUserDefaultServiceCenter setUserDefaultValue:sign WithKey:KAccountUserSign];
            [JBUserDefaultServiceCenter setUserDefaultValue:gender WithKey:KAccountUserGender];
            [JBUserDefaultServiceCenter setUserDefaultValue:provinceName WithKey:KAccountUserProvinceName];
            [JBUserDefaultServiceCenter setUserDefaultValue:provinceCode WithKey:KAccountUserProvinceCode];
            [JBUserDefaultServiceCenter setUserDefaultValue:cityName WithKey:KAccountUserCityName];
            [JBUserDefaultServiceCenter setUserDefaultValue:cityCode WithKey:KAccountUserCityCode];
        
        }
    });
}


- (void)updateNickName:(NSString *)nickName
            headImgurl:(NSString *)headImgurl
                  sign:(NSString *)sign gender:(NSString *)gender
          provinceName:(NSString *)provinceName
          provinceCode:(NSString *)provinceCode
              cityName:(NSString *)cityName cityCode:(NSString *)cityCode
{
    if (nickName) {
        self.accountUserNickName = nickName;
        [JBUserDefaultServiceCenter setUserDefaultValue:nickName WithKey:KAccountUserNickName];
    }
    
    if (headImgurl) {
        self.accountUserHeadImgurl = headImgurl;
        [JBUserDefaultServiceCenter setUserDefaultValue:headImgurl WithKey:KAccountUserHeadImgurl];
    }
    
    if (sign) {
        self.accountUserSign = sign;
        [JBUserDefaultServiceCenter setUserDefaultValue:sign WithKey:KAccountUserSign];
    }
    
    if (gender) {
        self.accountUserGender = gender;
        [JBUserDefaultServiceCenter setUserDefaultValue:gender WithKey:KAccountUserGender];
    }
    
    if (provinceName) {
        self.accountUserProvinceName = provinceName;
        [JBUserDefaultServiceCenter setUserDefaultValue:provinceName WithKey:KAccountUserProvinceName];
    }
    
    if (provinceCode) {
        self.accountUserProvinceCode = provinceCode;
        [JBUserDefaultServiceCenter setUserDefaultValue:provinceCode WithKey:KAccountUserProvinceCode];
    }
    
    if (cityName) {
        self.accountUserCityName = cityName;
        [JBUserDefaultServiceCenter setUserDefaultValue:cityName WithKey:KAccountUserCityName];
    }
    
    if (cityCode) {
        self.accountUserCityCode = cityCode;
        [JBUserDefaultServiceCenter setUserDefaultValue:cityCode WithKey:KAccountUserCityCode];
    }
}


- (void)logout
{
    self.logined = NO;
    self.accountUserId = defaultUserId;
    
    [JBUserDefaultServiceCenter setUserDefaultValue:[NSNumber numberWithBool:NO] WithKey:kLogined];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:kAccountLoginType];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:kAccountUserId];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:kAccountUserName];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:kAccountPassword];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserNickName];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserHeadImgurl];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserSign];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserGender];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserProvinceName];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserProvinceCode];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserCityName];
    [JBUserDefaultServiceCenter deleteUserDefaultValueByKey:KAccountUserCityCode];
}

- (void)userNickName:(NSString *)userNickName
{
    self.accountUserNickName=userNickName;
    [JBUserDefaultServiceCenter setUserDefaultValue:userNickName WithKey:KAccountUserNickName];
}
@end